package com.example.mobile1920605114zyh0603;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private Button btnMainPlayer;
    private Button btnMainPause;
    private Button btnMainReplay;
    private Button btnMainStop;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initMusic();
        btnMainPlayer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer == null) {
                    mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.old_boy);
                }
                mediaPlayer.start();
            }
        });
    btnMainPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
     if (mediaPlayer.isPlaying()){
         mediaPlayer.pause();
     }
        }
    });
    btnMainReplay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                mediaPlayer.seekTo(0);
                mediaPlayer.start();

            }
        }
    });
        btnMainStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer!=null && mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                    mediaPlayer = null;
                }
            }
        });

    }
    private void initMusic() {
        mediaPlayer = MediaPlayer.create(this, R.raw.old_boy);
//
    }

    private void initView() {
        textView = findViewById(R.id.textView);
        btnMainPlayer = findViewById(R.id.btnMainPlayer);
        btnMainPause = findViewById(R.id.btnMainPause);
        btnMainReplay = findViewById(R.id.btnMainReplay);
        btnMainStop = findViewById(R.id.btnMainStop);
    }
}
